
//返回SELECT被选中的option的text
function soptxt(id)
{
	return $('#'+id+' option[value='+$('#'+id).val()+']').text();
}

//验证输入文字为整形
function chkdig_int(obj)
{
	if(! $(obj).attr('_chkdig_init'))
	{
		var a = function(){chkdig_int(obj);};
		//$(obj).mouseover(a);
		//$(obj).keydown(a);
		//$(obj).keyup(a);
		$(obj).blur(a);
		$(obj).focus(a);
		$(obj).attr('_chkdig_init', true);
	}
	var t=1* obj.value.replace(/[^\d\-]+/,'');
	obj.value=t?t:'';
}
function chkdig_float(obj, scale)
{
	if(!scale) scale=2;
	if(! $(obj).attr('_chkdig_float'))
	{
		var a = function(){chkdig_float(obj, scale);};
		//$(obj).mouseover(a);
		//$(obj).keydown(a);
		//$(obj).keyup(a);
		$(obj).blur(a);
		$(obj).focus(a);
		$(obj).attr('_chkdig_float', true);
	}
	var t=(1 * obj.value.replace(/[^\d\.\-]+/,''));
	obj.value= t?t.toFixed(scale):'';
}

//显示出错信息/可控制是否自动隐藏信息
function showMessage(msg,objid)
{
alert(msg);return ;
	if(!objid) objid="result";
	$('#'+objid).html(msg);
	$('#'+objid).show(300);
	setTimeout("$('#"+objid+"').hide(300)", (arguments.length==3)?(arguments[2]?arguments[2]:3600000):4000);
}
function showMessage2(msg,objid)
{
	if(!objid) objid="result";
	$('#'+objid).html(msg);
	$('#'+objid).show(300);
	setTimeout("$('#"+objid+"').hide(300)", (arguments.length==3)?(arguments[2]?arguments[2]:3600000):4000);
}

//扩展ajaxForm，支持提交表单前加验证函数
var L_formid = null;
var L_subbtn = null;
function ajax_form(frmid, valifunc, calbak) /* 加第四个参数，为true表示不进行form.reset() */
{
	var reset	=	(arguments.length>=4 && arguments[3]) ? true: false;
	$('#'+frmid).bind('submit', function(){
		if(valifunc && !valifunc())	return false;
		if(! $('#'+frmid+' input[name=ajax]').val())
			$('#'+frmid).append('<input type="hidden" name="ajax" value="1" />');
		$(this).ajaxSubmit(calbak);
		if(!location.href.match(/id\/[1-9]+/))
		{
			if(!reset) L_formid = frmid;
		}
		$('#'+frmid+' input[type=submit]').each(function(i,o){
			o.disabled=true;
			L_subbtn = o.id?o.id:(o.id='lsbtn_'+Math.round(Math.random()*1000));
			o.className = "buttonplain";
		});
		return false;
	});
}
//ajaxForm返回结果处理函数
function afresult(dat)
{
	free_lsbtn();
	try{
		var jsn = eval(dat);
		if(jsn.result=='ok')
		{
			if(L_formid!=null)
			{
				document.getElementById(L_formid).reset();
				L_formid = null;
			}
			showMessage("操作成功!");
		}
		else
			showMessage(jsn.message);
		if(jsn.direct){
			setTimeout(parent.location.href=jsn.direct, 3000);
		}
	}catch(e){
		showMessage('服务器发生异常，返回结果未知!');
	}
}
//释放form按钮
function free_lsbtn(){
	if(L_subbtn){
		$('#'+L_subbtn).attr('disabled', false);
		document.getElementById(L_subbtn).className = "button";
		L_subbtn = null;
	}
}

//图片大小缩略
function resizepic(picname,width_v,height_v){
	if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){
		var backw=width_v;
		var backh=height_v;
	}else{
		var backw=width_v+8;
		var backh=height_v+8;
	}
	var i = String(Math.floor(Math.random()*10000)+1); 
	document.writeln("<div style='width:"+backw+"px;height:"+backh+"px;padding:3px;border:1px solid #a1a1a1;backgournd:#fff;'>");
	document.writeln("<div style='width:"+width_v+"px;height:"+height_v+"px;overflow:hidden;'>");
	document.writeln("<img src='"+picname+"' id='img"+i+"'/></div></div>");

	$("#img"+i).ready(function(){
		try{
			var pic = this;
			if((width_v/height_v)>(pic.offsetWidth/pic.offsetHeight)){
				pic.width=width_v;
			}else{
				pic.height=height_v;
			}
		}catch(e){}
	});
}

function   out(e)
{
	var   oObj   =   e.target || window.event.srcElement;
	if(oObj.tagName.toLowerCase()   ==   "td")
	{
		var   oTr   =   oObj.parentNode;
		if(!oTr.tag)
		oTr.className   =   "out";
	}
}
function   over(e)
{
	var   oObj   =   e.target || window.event.srcElement;
	if(oObj.tagName.toLowerCase()   ==   "td")
	{
		var   oTr   =   oObj.parentNode;
		if(!oTr.tag)
		oTr.className   =   "over";
	}
}

//显示和刷新验证码
window.vCodeInit = 0;
function firstVCode()
{
	if(!vCodeInit){ fleshVerify();vCodeInit=1; }
}
function fleshVerify(){
	$('#verifyImg').attr('src', URL_VERIFYIMG+(new Date().getTime()));
	$('#verifyImg').show();
	vCodeInit=1;
}

//登陆页处理事件
function initLoginPage(){
	$('#form1').ajaxForm(function(result){
		$('#form1').get(0).reset();
		$('input[name=account]').focus();
		try{
			var jsn = eval("("+result+")");
		}catch(e){
			showMessage2('<span style="color:blue"><IMG SRC="'+SKIN_PATH+'/Images/warn.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="" align="absmiddle">发生错误：'+result+'</span>', null, 10000);
			return ;
		}
		if(jsn.status==1)
		{
			showMessage2('<span style="color:blue"><IMG SRC="'+SKIN_PATH+'/Images/ok.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="" align="absmiddle" > 登录成功！准备跳转～</span>');
			setTimeout('location.reload()', 800);
		}
		else
		{
			showMessage2('<span style="color:blue"><IMG SRC="'+SKIN_PATH+'/Images/warn.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="" align="absmiddle">'+(jsn.info?jsn.info:'')+'</span>');
			fleshVerify();
		}
	});
	$('#form1').submit(function(){
		showMessage2('<IMG SRC="'+SKIN_PATH+'/Images/ajaxloading.gif" WIDTH="16" HEIGHT="16" BORDER="0" ALT="" align="absmiddle"> 登录中～',0);
		return false;
	});
	$('input[name=account]').focus();
}

//切换菜单显示
//param1:菜单数字ID， param2:高亮的子ID， param3:显示动画秒数
function switch_menu(mid)
{
	if(!mid) return;
	var submid=0, sec=200;
	if(arguments.length>1)
	{
		submid = arguments[1];
		if(arguments.length>2)
			sec = arguments[2];
	}
	if($("#submenu_"+mid).attr("inshow")==1)
	{
		$("#submenu_"+mid).attr("inshow", 0);
		if(sec) $("#submenu_"+mid).hide(sec); else $("#submenu_"+mid).hide();
	}
	else
	{
		$("#submenu_"+mid).attr("inshow", 1);
		if(sec) $("#submenu_"+mid).show(sec); else $("#submenu_"+mid).show();
	}
}

//输入回车进行搜索（避免在input里自动提交），并指定自定义的回车接收事件
function register_search_key(id, func)
{
	$("#"+id).keydown(function(e){
		if(e.which==13){
			if(func) setTimeout(func, 100);
			return false;
		}
	});
}

/**  级联选择服务位置信息，调用方法: $('#xxx').change(rec_area);  */
function rec_area()
{
	var _id	=	this.id+'_';
	$('select[id*='+_id+']').remove();
	if(!$(this).val())	return;
	var _o	=	this;
	_id		=	this.id+'_'+$(this).val();
	$.get(URL_REC_AREA+$(this).val(),function(data){
		try{
			var jsn	=	eval(data);
			if(jsn.areas)
			{
				$(_o).after('<select name="'+_o.name+'" id="'+_id+'"><option value=""></option></select>');
				var bdy	=	document.getElementById(_id);
				$(bdy).change(rec_area);
				$.each(jsn.areas, function(i,o){
					$(bdy).append('<option value="'+o.area_id+'">'+o.title+'</option>');
				});
			}
			var customer_id	=	document.getElementById('customer_id');
			if(customer_id && jsn.customer){
				$(customer_id).empty();
				L_customer[jsn.customer.customer_id]	=	jsn.customer;
				$(customer_id).append('<option value="">--检索结果--</option><option value="'+jsn.customer.customer_id+'">'+jsn.customer.title+'</option>');
			}
			else
			{
				//var place	=	document.getElementsByName('place')[0];
				//if(place) place.value = jsn.customer.title;
			}
		}catch(e){}
	});
}
/**  级联部门小组 */
function dept_grp()
{
	$('#group_id').empty();
	var dept_id	=	$('#dept_id').val();
	if(dept_id)
	{
		$.get(URL_DEPTGROUP+dept_id,function(data){
			try{
				var jsn	=	eval(data);
				var bdy	=	document.getElementById('group_id');
				if(jsn.groups)
				{
					$('#group_id').append('<option value="">--小组--</option>');
					$.each(jsn.groups, function(i,o){
						$(bdy).append('<option value="'+o.group_id+'">'+o.title+'</option>');
					});
				}
				else
					$('#group_id').append('<option value="">--无结果--</option>');
			}catch(e){
				$('#group_id').append('<option value="">--无结果--</option>');
			}
		});
	}
}

/** 级联 小组 服务人 */
function group_worker()
{
	var group_id	=	$('#group_id').val();
	if(group_id)
	{
		$('#worker_id').empty();
		$.get(URL_GROUPWORKER+group_id, function(data){ $('#worker_id').html(_worker_list(data)); });
	}
}
/** 级联 部门 服务人 */
function dept_worker()
{
	var dept_tag_id	=	(arguments[0]?arguments[0]:'dept_id');
	var dept_id		=	$('#'+dept_tag_id).val();
	var worker_tag_id	=	(arguments[1]?arguments[1]:'worker_id');
	if(dept_id)
	{
		$('#'+worker_tag_id).empty();
		$.get(URL_DEPTWORKER+dept_id, function(data){ $('#'+worker_tag_id).html(_worker_list(data)); });
	}
}
var	L_worker_job	=	new Array();	//记录服务人工作状态
/** 私有函数 */
function _worker_list(data)
{
	try{
		var jsn	=	eval(data);
		if(jsn.workers)
		{
			var h	=	'<option value="">--选择人员--</option>';
			$.each(jsn.workers, function(i,o){
				L_worker_job[o.worker_id]	=	o.jobs;
				h	+=	('<option value="'+o.worker_id+'">'+o.nickname);
				if(o.is_leader>0)
				{
					h	+=	' (管';
					h	+=	o.jobs?(',工:'+o.jobs.length)+')':')';
				}
				else if(o.jobs)
				{
					h	+=	' (工:'+o.jobs.length+')';
				}
				h	+=	'</option>';
			});
			return h;
		}else{
			return '<option value="">--无结果--</option>';
		}
	}catch(e){
		return '<option value="">--无结果--</option>';
	}
}

/** 添加服务人到工单 */
function add_worker()
{
	var worker_id	=	$('#worker_id').val();
	if(!worker_id)	return ;

	if(document.getElementById('selwrk_'+worker_id))
	{
		showMessage("请勿重复选择相同服务人.");
		return;
	}

	var h	=	'';
	h	+=	'<tr id="selwrk_'+worker_id+'"><td>';
	h	+=	soptxt('worker_id')+'</td><td>';
	h	+=	soptxt('dept_id')+'</td><td>';
	h	+=	($('#group_id').val()?soptxt('group_id'):'')+'</td><td>';
	var job	=	L_worker_job[worker_id];
	if(job)
	{
		$.each(job,function(i,o){
			h	+=	'<a href="'+URL_VIEWJOB_ID+o['job_id']+'" target="_blank">'+o['job_serial']+'</a> ';
		});
	}
	else	h	+=	'&nbsp;';
	h	+=	'</td><td>';
	h	+=	'<a href="#a" onclick="$(\'#selwrk_'+worker_id+'\').remove()">取消</a></td>';
	h	+=	'<input type="hidden" name="worker[]" value="'+worker_id+'" />';
	h	+=	'</tr>';
	$('#worker_list').append(h);
}

var L_stuff = new Array();
//检索材料信息
function search_stuff()
{
	var v = $('#key_value').val();
	if(v.length<1)
	{
		showMessage("请输入至少一个字符的检索条件.");
		return;
	}

	$('#stuff_id').empty();
	$.post(URL_SRCHSTUFF,{"v":v},function(data){
		try{
			var jsn	=	eval(data);
			var bdy	=	document.getElementById('stuff_id');
			if(jsn.stuffs){
				$('#stuff_id').append('<option value="">--检索结果--</option>');
				$.each(jsn.stuffs, function(i,o){
					$('#stuff_id').append('<option value="'+o['stuff_id']+'">'+o['title']+' # '+o['price']+'</option>');
					L_stuff[o['stuff_id']] = o;
				});
			}
			else
				$('#stuff_id').append('<option value="">--无结果--</option>');
		}catch(e){}
	});
}

//添加材料到工单
function add_stuff()
{
	var stuff_id	=	$('#stuff_id').val();
	if(!stuff_id)	return ;

	if(document.getElementById('selstf_'+stuff_id))
	{
		showMessage("请勿重复选择同型号材料.");
		return;
	}

	var stf	= L_stuff[stuff_id];
	var h	=	'';
	h	+=	'<tr id="selstf_'+stuff_id+'"><td>';
	h	+=	stf['title']+'</td><td>';
	h	+=	stf['price']+'</td><td>';
	h	+=	stf['unit']+'</td><td>';
	h	+=	'<input type="text" name="stuff_id['+stuff_id+']" stfprc="'+stf['price']+'" value="1" size="5" onfocus="chkdig_int(this)" onblur="do_stuff_total()"/>';
	h	+=	'</td><td>';
	h	+=	'<a href="#a" onclick="$(\'#selstf_'+stuff_id+'\').remove();do_stuff_total();">取消</a></td>';
	h	+=	'</tr>';
	$('#stuff_list').append(h);
	do_stuff_total();
}
//计算材料总价
function do_stuff_total()
{
	var total = 0;
	$('#stuff_list input').each(function(i,o){
		total += (o.value* $(o).attr('stfprc'));
	});
	total = Math.round(total*100)/100;
	$('#stuff_fund').html("总价："+total);
}


var L_customer = new Array();
//检索客户信息
function search_customer()
{
	var v = $('#key_value').val();
	if(v.length<2)
	{
		showMessage("请输入至少两个字符的检索条件.");
		return;
	}
	$('#customer_id').empty();
	$.post(URL_SRCHCUSTOMER,{"v":v},function(data){
		try{
			var jsn	=	eval(data);
			var bdy	=	document.getElementById('customer_id');
			if(jsn.customers){
				$('#customer_id').append('<option value="">--检索结果--</option>');
				$.each(jsn.customers, function(i,o){
					$('#customer_id').append('<option value="'+o.customer_id+'">'+o.title+'</option>');
					L_customer[o.customer_id] = o;
				});
			}
			else
				$('#customer_id').append('<option value="">--无结果--</option>');
		}catch(e){}
	});
}
//写客户联系信息到联系信息输入框
function chs_cust()
{
	var v=$('#customer_id').val();
	if(v)
	{
		if(L_customer[v])
		{
			var l	=	L_customer[v].linkman ? L_customer[v].linkman : '';
			l		+=	L_customer[v].phone ? ' '+L_customer[v].phone : '';
			/*
			var lk	=	L_customer[v].linkman;
			for(var i=0;i<lk.length;i++){
				l	+=	', '+lk[i].m_phone+' '+lk[i].name;
			}
			l	=	l.substr(2);
			*/
		}
		else
			var l	=	'';
		$('#customer_link').val(l);
	}
	else
		$('#customer_link').val('');
}

//记录开启菜单ID
function archor(aid)
{
	$.cookie('_archor_', aid, {path:'/',secure:false});
}

